<?php

class ShopController extends Zend_Controller_Action
{

    public function init()
    {
		/* Initialize action controller here */
		$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/shop.css');
		//设置title
		$this->view->headTitle("好店铺");
    }

    public function indexAction()
    {
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$selecter=$db->select()->from('txy_shop',array('sid','url_spell','title','shop_bar'))
								->where('(url_spell is not null and url_spell !="") and (shop_bar is not null and shop_bar !="")');
		$adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage(48);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
		
    }
    
    public function contentAction(){
        //设置首页引用js
        $this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
        $this->view->headScript()->appendFile($this->view->baseUrl().'/js/slideFlash.js');
        $this->_helper->layout()->setLayout('shop');
        //$this->view->specialProduct=1;
    	//$sid=$this->getRequest()->getParam('id');
    	$url=$this->getRequest()->getParam('url');
    	$db_shop=new Application_Model_DbTable_Shop();
    	$shop=$db_shop->getShopByUrl($url);
        $dbSlideFlash=new Application_Model_DbTable_SlideFlash();
        $slideFlash=$dbSlideFlash->getSlideFlashByShopId($shop->sid);
        if(count($slideFlash)>=1){
            $this->view->slideFlash=$slideFlash;
            $this->view->active=1;
        }
    	//设置title
		$this->view->headTitle()->prepend($shop->title);
		//设置关键词（seo）
		$this->view->headMeta()->appendName('keywords', $shop->key_words);
		//设置描述（seo）
		$this->view->headMeta()->appendName('description',$shop->description);
		$this->view->shop=$shop;
		
		if(null!=$shop->shop_bar&&""!=$shop->shop_bar){
		    $this->view->shopBar=$shop->shop_bar;
		}
		
		$config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$selecter=$db->select()->from('txy_taobao_items',array('num_iid','title','item_url_spell','price','is_promotion','promotion_price','promotion_name','pic_url','freight_payer'))
								->where("nick='".$shop->nick."'")->order('count_num desc')->limit(48);
		$res=$db->fetchAll($selecter);
		$this->view->products=$res;
		
		$counterSelect=$db->select()->from('txy_taobao_items',array('count(*)'))->where("nick='".$shop->nick."'")->order('count_num desc')->limit(48);
		$countNum=$db->fetchOne($counterSelect);
		if($countNum>48){
		    $this->view->more=1;
		}
		
	/*	$adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage(48);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;*/
    	
    }
    public function searchAction(){
        
        $url=trim($this->getRequest()->getParam('url'));
    	$db_shop=new Application_Model_DbTable_Shop();
    	$shop=$db_shop->getShopByUrl($url);
		$this->view->shop=$shop;
    	
    	$tags=$this->getRequest()->getParam('tags');
        
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        
        $selecterLevel0=$db->select()->from('txy_items_join_character',array('level0'))->where("nick = '".$shop->nick."'")->group('level0');
        $res0=$db->fetchAll($selecterLevel0);
        $this->view->level0=Txy_Util_Character::uniqueCharacter($res0, 'level0');
        
        $selecterLevel1=$db->select()->from('txy_items_join_character',array('level1'))->where("nick = '".$shop->nick."'")->group('level1');
        $res1=$db->fetchAll($selecterLevel1);
        $this->view->level1=Txy_Util_Character::uniqueCharacter($res1, 'level1');
        
        $selecterLevel2=$db->select()->from('txy_items_join_character',array('level2'))->where("nick = '".$shop->nick."'")->group('level2');
        $res2=$db->fetchAll($selecterLevel2);
        $this->view->level2=Txy_Util_Character::uniqueCharacter($res2, 'level2');
        
        $selecterLevel3=$db->select()->from('txy_items_join_character',array('level3'))->where("nick = '".$shop->nick."'")->group('level3');
        $res3=$db->fetchAll($selecterLevel3);
        $this->view->level3=Txy_Util_Character::uniqueCharacter($res3, 'level3');
        
        $selecterLevel4=$db->select()->from('txy_items_join_character',array('level4'))->where("nick = '".$shop->nick."'")->group('level4');
        $res4=$db->fetchAll($selecterLevel4);
        $this->view->level4=Txy_Util_Character::uniqueCharacter($res4, 'level4');
        if(null!=$tags){
            if(stristr($tags, "-")){
    	        $arrayTags=explode("-", $tags);
    	        foreach ($arrayTags as $tag){
    	             $res=Txy_Util_Character::getCharacterLevelRes($tag);
    	             $currentTags[$res['level']]=$res['name'];
    	        }
	            $this->view->currentTags=$currentTags;
                //设置title
                $title=str_replace("-", " - ", $tags);
	            $this->view->headTitle('标签分类 - '.$title);
	            //设置关键词（seo）
	            $keywords=str_replace("-", ",", $tags);
	            $this->view->headMeta()->appendName('keywords', $keywords);
	            //设置描述（seo）
	            $this->view->headMeta()->appendName('description','通过'.$keywords.'标签组合对商品进行分类筛选所得商品列表。');
            }else{
                $res=Txy_Util_Character::getCharacterLevelRes($tags);
                $currentTags[$res['level']]=$res['name'];
                $this->view->currentTags=$currentTags;
                //设置title
	            $this->view->headTitle('标签分类 - '.$tags);
	            //设置关键词（seo）
	            $this->view->headMeta()->appendName('keywords', $tags);
	            //设置描述（seo）
	            $this->view->headMeta()->appendName('description','通过'.$tags.'标签对商品进行分类筛选所得商品列表。');
            }
            $sqlWhere=Txy_Util_Character::getCharacterWhereSql($tags);
            $sqlWhere.=" and nick = '".$shop->nick."'";
            $selecterNum=$db->select()->from('txy_items_join_character','count(*)')->where($sqlWhere);
            $resNum=$db->fetchOne($selecterNum);
            if((int)$resNum>0){
                $selecter=$db->select()->from('txy_items_join_character')->where($sqlWhere);
                $this->view->ResultsNum=$resNum;
            }else{
                $selecter=$db->select()->from('txy_taobao_items')->where("nick = '".$shop->nick."'")->order("count_num desc");
                $this->view->ResultsNum=0;
            }
            
        }else{
            $selecter=$db->select()->from('txy_taobao_items')->where("nick = '".$shop->nick."'")->order("count_num desc");
            //设置title
            $this->view->headTitle('标签分类');
            //设置关键词（seo）
            $this->view->headMeta()->appendName('keywords', "标签分类,商品标签,商品分类");
            //设置描述（seo）
            $this->view->headMeta()->appendName('description','通过商品标签对商品进行分类，用户通过标签组合可以筛选期望的目标商品。');
            
            
        }
            $adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(20);
            $paginator->setCurrentPageNumber($this->_getParam('page'));
            $this->view->paginator = $paginator;
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
/*        $page=$this->getRequest()->getParam('page');
        echo "page".$page;
    	//设置title
		$this->view->headTitle($shop->title);
		//设置关键词（seo）
		$this->view->headMeta()->appendName('keywords', $shop->key_words);
		//设置描述（seo）
		$this->view->headMeta()->appendName('description',$shop->description);
		
		if(null!=$shop->shop_bar&&""!=$shop->shop_bar){
		    $this->view->shopBar=$shop->shop_bar;
		}
		
		$config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$selecter=$db->select()->from('txy_taobao_items',array('num_iid','title','item_url_spell','price','is_promotion','promotion_price','promotion_name','pic_url','freight_payer'))
								->where("nick='".$shop->nick."'")->order('count_num desc');
		$adapter = new Zend_Paginator_Adapter_DbSelect($selecter);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage(48);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;*/
    }


}

